import os,random

from locust import HttpUser,TaskSet,task

class Woniusales(TaskSet):

    @task
    def get_home(self):
        #发送首页的请求
        with self.client.get('/',catch_response=True) as resp:
            if "孵化园" in resp.text:
                resp.success()
            else:
                resp.failure()
    @task
    def login(self):
        # 登录
        data={"username":"admin","password":"admin123","verifycode":"0000"}
        with self.client.post('/user/login',data,catch_response=True) as resp:
            if "login-fail" in resp.text:
                resp.failure("loginfail")
            else:
                resp.success()
                # self.client.get("/user/logout")
    @task
    def add(self):
        # 添加会员
        data = {"username": "admin", "password": "admin123", "verifycode": "0000"}
        param = {
            "customername": "zhf",
            "customerphone": f"130{random.randint(00000000, 99999999)}",
            "childsex": "男",
            "childdate": "2021-04-25",
            "creditkids": 10,
            "creditcloth": 0
        }
        with self.client.post('/user/login', data, catch_response=True) as resp:
            with self.client.post("/customer/add",data=param,catch_response=True) as resp:
                if "add-successful" in resp.text:
                    resp.success()
                else:
                    resp.failure("logoutfail")
    @task
    def logout(self):
        # 注销
        with self.client.get('/user/logout',catch_response=True) as resp:
            if "尚未登录" in resp.text:
                resp.success()
            else:
                resp.failure()


class Website(HttpUser):
    tasks = [Woniusales]
    min_wait = 3000
    max_wait = 6000

if __name__ == '__main__':
    os.system("locust -f E:\woniu\Demo\locust_demo.py --host=http://localhost:8080/woniusales")



